package com.me.libgdx_test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;

public class ClassMarketItem extends Actor{
	private Sprite picture;
	private String description, title;
	private Point dimensions,location;
	private ClassSkinReference skin;
	private ClassLogDisplay logger;
	private String button_text;
	private Object object,activity_object, director_object;
	private Method method;
	private int click_index;
	private final ActivityMain game;
	private Label description_label;
	private ClassUnlockCost cost;
	private ClassAchievementMarket object_cost;
	private TextButton info_button;
	private Stage stage;
	ClassMarketItem(Stage stage, Sprite picture, String title, String description, Point dimensions, Point location, 
			ClassSkinReference skin, String button_text, Object object, Method method, 
			int click_index, ActivityMain game, Object activity_object, Object director_object, 
			ClassUnlockCost cost){
		this.picture = new Sprite(picture);
		this.title = title;
		this.description = description;
		this.skin = skin;
		this.dimensions = new Point(dimensions.x,dimensions.y);
		this.location = new Point(location.x,location.y);
		this.button_text = button_text;
		
		this.stage = stage;
		this.object = object;
		this.method = method;
		this.click_index = click_index;
		logger = new ClassLogDisplay("ClassMarketItem");
		this.game = game;
		this.cost = cost;
		this.activity_object = activity_object;
		this.director_object = director_object;
		description_label = new Label(description, skin.get_skin());
		info_button = new TextButton("Info",skin.get_skin());
	}
	public void update_cost(ClassUnlockCost cost){
		this.cost = cost;
	}
	public int get_click_index(){
		return click_index;
	}
	public int get_click_revised_click_index(){
		return click_index-8;
	}
	public void update_description(String description){
		this.description = description;
		description_label.setText(this.description);
	}
	public void construct_market_item(Group switch_group,float y_offset, float font_size){
		
		int element_y = (int) (location.y/2 - y_offset);
		int padding = dimensions.x/40;
		
		final Image market_item_image = new Image(picture);
		//market_item_image.setPosition(dimensions.x/10, element_y);
		market_item_image.setPosition(location.x, element_y);
		market_item_image.setSize(dimensions.x/12,dimensions.y/12);
		switch_group.addActor(market_item_image);
		
		padding += market_item_image.getWidth();
				
		//logger.print(new Point((int)table_pointer.getWidth(),(int)table_pointer.getHeight()));
		
		/*Table market_subTable = new Table();
		market_subTable.setFillParent(true);
		market_subTable.align(Align.left);
		
		//table_pointer.add(market_subTable);*/
		
		/*description_label = new Label(description, skin.get_skin());
		description_label.setFontScale(font_size);
		description_label.setWrap(true);
		description_label.setPosition(location.x+padding, element_y);
		description_label.setSize(4*dimensions.x/10,dimensions.y/10);
		
		switch_group.addActor(description_label);
		padding += description_label.getWidth()/2 + dimensions.x/40;*/
		
		
		info_button = new TextButton(title + " - Info",skin.get_button_skin());
		info_button.setPosition(location.x+padding, element_y);
		info_button.setSize(4*dimensions.x/10,dimensions.y/10);
		
		info_button.addListener(new ChangeListener() {
			public void changed (ChangeEvent event, Actor actor) {
				new ClassDialogBox(stage, title, description, game.skin.get_alternate_skin(), true);
			}
		});
		
		switch_group.addActor(info_button);
		
		padding += info_button.getWidth()/2 + dimensions.x/40;
		
		//table_pointer.add(description_label).size(dimensions.x/3,dimensions.y/10).pad(10).center();
		//logger.print(new Point((int)table_pointer.getWidth(),(int)table_pointer.getHeight()));
		
		object_cost = new ClassAchievementMarket(game,dimensions,cost);
		object_cost.get_achievement_group().setPosition(location.x+padding, element_y + dimensions.y/100);
		switch_group.addActor(object_cost.get_achievement_group());
		
		logger.print("Achievement Width: " + object_cost.get_width());
		
		padding += object_cost.get_width() + dimensions.x/40;
		
		TextButton Tavern_Button = new TextButton(button_text, skin.get_button_skin());

		Tavern_Button.setPosition(location.x+padding, element_y);
		Tavern_Button.setSize(dimensions.x/8,dimensions.y/15);
		
		Tavern_Button.addListener(new ChangeListener() {
			public void changed (ChangeEvent event, Actor actor) {
				Object[] parameters = new Object[1];
				if(method.getName() == "unlock_upgrade"){
					parameters[0] = get_click_revised_click_index();
				}else{
					parameters[0] = click_index;
				}

				try {
					logger.print("Button Clicked!");
					logger.print("Trying method: " + method.getName());
					//Bank loans give you stuff for real money, so we dont need to calculate the cost here
					if(game.unlockable.get_unlockable_prototypes().get_factory().check_purchase(cost) || method.getName().equals("submitPurchase")){
						logger.print("Invoking method: " + method.getName());
						if(method.getName().equals("submitPurchase")){
							
							logger.print("In get purchase");
							
							Class[] parameterTypes = new Class[2];
					        parameterTypes[0] = int.class;
					        parameterTypes[1] = String.class;
					        
					        logger.print("Before loan with: " + click_index);
					        
					        ClassDungeonBank loan = (new ClassReferenceDrawable(0,0,0,game.unlockable)).bank_objects[click_index];
					        
					        logger.print("Unlock Credits: " + loan.get_unlock_level());
					        logger.print("Unlock Item Name: " + loan.get_play_id());
					        
							Object[] alt_parameters = new Object[4];
							alt_parameters[0] = loan.get_play_id();
							alt_parameters[1] = loan.get_unlock_level();
							alt_parameters[2] = game.marketScreen;
							alt_parameters[3] = game.marketScreen.getClass().getMethod("item_purchased", parameterTypes);
					        
							logger.print("Invoking Billing Methods...");
							
							method.invoke(object, alt_parameters);
						}else{
							game.unlockable.get_unlockable_prototypes().get_factory().make_purchase(cost);
							method.invoke(object, parameters);
							
							//Reset the activities that reference the unlockables
							game.reset_activities();
							
							//Send new data to the server so that we remember it
							game.unlockable.save_data_to_server();
							
					        object_cost.populate_achievement_object();
							
							Method set_items = activity_object.getClass().getMethod("set_test_items", new Class[0]);
					        set_items.invoke(activity_object, new Object[0]);
					        
					        Method direct_rows = director_object.getClass().getMethod("set_last_options", new Class[0]);
					        direct_rows.invoke(director_object, new Object[0]);
						}
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		switch_group.addActor(Tavern_Button);
	}
	public String get_description(){
		return description;
	}
}
